package org.fisco.bcos.evidence.controller;

import org.fisco.bcos.evidence.bean.BcosEvidence;
import org.fisco.bcos.evidence.client.EvidenceTableClient;
import org.fisco.bcos.evidence.config.ResultCode;
import org.fisco.bcos.evidence.config.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/evidence")
public class EvidenceTableClientController {

    @Autowired
    private EvidenceTableClient client;

    ///new evidence
    @RequestMapping(method = {RequestMethod.POST}, value = "/addEvidence")
    public ResultJson addEvidence(
            @RequestParam(value = "key", required = true) String key,
            @RequestParam(value = "data") String data,
            @RequestParam(value = "owner") String owner
    ) {

        Object resp = null;
        try {
            BcosEvidence evidence = new BcosEvidence();
            evidence.setKey(key);
            evidence.setData(data);
            evidence.setOwner(owner);
            if (client.addEvidence(evidence)) {
                resp = client.getEvidence(key);
            }
            if (resp == null) {
                return ResultJson.ok();
            }
        } catch (Exception e) {
            return ResultJson.failure(ResultCode.ExecutionException, e.toString());
        }
        return ResultJson.ok(resp, "成功");

    }

    ///get evidence
    @GetMapping("/getEvidence")
    public ResultJson getEvidence(
            @RequestParam(value = "key", required = true) String key) {
        Object resp = null;
        try {
            resp = client.getEvidence(key);
            if (resp == null) {
                return ResultJson.ok();
            }
        } catch (Exception e) {
            return ResultJson.failure(ResultCode.ExecutionException, e.toString());
        }
        return ResultJson.ok(resp, "成功");
    }

    ///get By owner
    @GetMapping("/getEvidencesByOwner")
    public ResultJson getEvidencesByOwner(
            @RequestParam(value = "owner", required = true) String owner) {
        Object resp = null;
        try {
            resp = client.getEvidencesByOwner(owner);
            if (resp == null) {
                return ResultJson.ok();
            }
        } catch (Exception e) {
            return ResultJson.failure(ResultCode.ExecutionException, e.toString());
        }
        return ResultJson.ok(resp, "成功");
    }


    ////////////////////////////////////////////////////////////////////////
    ////////////////////////////////TEST////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////

    ///1. (test)insert
    @GetMapping("/add")
    public boolean add() throws Exception {
//        if (!initFlag) init();

        BcosEvidence evidence = new BcosEvidence();
        evidence.setKey("u0001");
        evidence.setData("123456");
        evidence.setOwner("tom&jerry");
        client.addEvidence(evidence);

        evidence.setKey("u0002");
        evidence.setData("123456222222");
        evidence.setOwner("tom&jerry");

        return client.addEvidence(evidence);
    }

    ///2. (test)get
    @GetMapping("/select")
    public BcosEvidence select() throws Exception {
//        if (!initFlag) init();
        return client.getEvidence("u0002");
    }

    ///3. (test)get By owner
    @GetMapping("/selectN")
    public List<BcosEvidence> selectByOwner() throws Exception {
//        if (!initFlag) init();
        return client.getEvidencesByOwner("tom&jerry");
    }


}

/**
 * http://localhost:8080/account/set
 * http://localhost:8080/account/get
 */
